<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主机名称" prop="hostName">
        <el-input
          v-model="queryParams.hostName"
          placeholder="请输入主机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机类型" prop="hostFlag">
          <el-select
            v-model="queryParams.hostFlag"
            placeholder="请选择主机类型"
            clearable
            size="small"
            style="width: 240px"
          >

            <el-option
              v-for="dict in hostFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="在线状态" prop="hostOline">
          <el-select
            v-model="queryParams.hostOline"
            placeholder="请选择主机在线状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in hostOlineOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:host:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:host:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:host:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:host:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hostList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主机名称" align="center" prop="hostName" />
      <el-table-column label="主机类型" align="center" prop="hostFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_host_flag" :value="scope.row.hostFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="主机在线状态" align="center" prop="hostOline">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_host_oline" :value="scope.row.hostOline"/>
        </template>
      </el-table-column>
      <el-table-column label="主机状态" align="center" prop="hostState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_host_state" :value="scope.row.hostState"/>
        </template>
      </el-table-column>
      <el-table-column label="主机版本" align="center" prop="version" />
      <el-table-column label="主机IP" align="center" prop="hostIp" />
      <el-table-column label="主机MAC地址" align="center" prop="hostMac" />
      <el-table-column label="主机硬盘序列号" align="center" prop="hostSn" />
      <el-table-column label="主机密级" align="center" prop="hostSec">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_host_sec" :value="scope.row.hostSec"/>
        </template>
      </el-table-column>
      <el-table-column label="主机注册时间" align="center" prop="hostRegDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hostRegDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主机激活时间" align="center" prop="hostActiveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hostActiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主机注销时间" align="center" prop="hostUnregDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hostUnregDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="lastModifyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastModifyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主机码" align="center" prop="hostCode" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="体征码" align="center" prop="feature" />
      <el-table-column label="外部数据源" align="center" prop="datasource" />
      <el-table-column label="外部数据源ID" align="center" prop="datasourceId" />
      <el-table-column label="主机系统" align="center" prop="hostSys" />
      <el-table-column label="最后上线时间" align="center" prop="lastOnlineDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastOnlineDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作系统版本" align="center" prop="osversioninfo" />
      <el-table-column label="主机IP" align="center" prop="hostIpval" />
      <el-table-column label="操作系统安装时间" align="center" prop="osInstalltime" />
      <el-table-column label="资产编号" align="center" prop="hostAssetsNo" />
      <el-table-column label="房间号" align="center" prop="hostRoomNo" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:host:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:host:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【主机】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="主机名称" prop="hostName">
              <el-input v-model="form.hostName" placeholder="请输入主机名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机类型" prop="hostFlag">
              <el-select
                v-model="form.hostFlag"
                placeholder="请选择主机类型"
                clearable
                size="small"
              >

                <el-option
                  v-for="dict in hostFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="Number(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机在线状态" prop="hostOline">
              <el-select
                v-model="form.hostOline"
                placeholder="请选择主机在线状态"
                clearable
                size="small"
              >

                <el-option
                  v-for="dict in hostOlineOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="Number(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机状态" prop="hostState">
              <el-select
                v-model="form.hostState"
                placeholder="请选择主机状态"
                clearable
                size="small"
              >

                <el-option
                  v-for="dict in hostStateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="Number(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主机版本" prop="version">
              <el-input v-model="form.version" placeholder="请输入主机版本" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机密级" prop="hostSec">
              <el-select
                v-model="form.hostSec"
                placeholder="请选择主机密级"
                clearable
                size="small"
              >

                <el-option
                  v-for="dict in hostSecOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="Number(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机IP" prop="hostIp">
              <el-input v-model="form.hostIp" placeholder="请输入主机IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机MAC地址" prop="hostMac">
              <el-input v-model="form.hostMac" placeholder="请输入主机MAC地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机硬盘序列号" prop="hostSn">
              <el-input v-model="form.hostSn" placeholder="请输入主机硬盘序列号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主机注册时间" prop="hostRegDate">
              <el-date-picker clearable
                              v-model="form.hostRegDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择主机注册时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机激活时间" prop="hostActiveDate">
              <el-date-picker clearable
                              v-model="form.hostActiveDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择主机激活时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机注销时间" prop="hostUnregDate">
              <el-date-picker clearable
                              v-model="form.hostUnregDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择主机注销时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后修改时间" prop="lastModifyDate">
              <el-date-picker clearable
                              v-model="form.lastModifyDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择最后修改时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机码" prop="hostCode">
              <el-input v-model="form.hostCode" placeholder="请输入主机码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="体征码" prop="feature">
              <el-input v-model="form.feature" placeholder="请输入体征码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外部数据源" prop="datasource">
              <el-input v-model="form.datasource" placeholder="请输入外部数据源" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主机系统" prop="hostSys">
              <el-input v-model="form.hostSys" placeholder="请输入主机系统" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="操作系统安装时间" prop="osInstalltime">
              <el-input v-model="form.osInstalltime" placeholder="请输入操作系统安装时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产编号" prop="hostAssetsNo">
              <el-input v-model="form.hostAssetsNo" placeholder="请输入资产编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房间号" prop="hostRoomNo">
              <el-input v-model="form.hostRoomNo" placeholder="请输入房间号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作系统版本" prop="osversioninfo">
              <el-input v-model="form.osversioninfo" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHost, getHost, delHost, addHost, updateHost } from "@/api/system/host";

export default {
  name: "Host",
  dicts: ['sys_host_flag', 'sys_host_oline','sys_host_state','sys_host_sec'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【主机】表格数据
      hostList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hostName: null,
        hostFlag: null,
        hostOline: null,
        hostState: null,
        version: null,
        hostIp: null,
        hostMac: null,
        hostSn: null,
        hostSec: null,
        hostRegDate: null,
        hostActiveDate: null,
        hostUnregDate: null,
        lastModifyDate: null,
        userId: null,
        hostCode: null,
        feature: null,
        datasource: null,
        datasourceId: null,
        isDelete: null,
        hostSys: null,
        lastOnlineDate: null,
        osversioninfo: null,
        createDate: null,
        hostIpval: null,
        osInstalltime: null,
        hostAssetsNo: null,
        hostRoomNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hostFlag: [
          { required: true, message: "主机类型不能为空", trigger: "blur" }
        ],
        hostOline: [
          { required: true, message: "主机在线状态不能为空", trigger: "blur" }
        ],
        hostState: [
          { required: true, message: "主机状态不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "主机版本不能为空", trigger: "blur" }
        ],
      },
      // 数据字典
      hostFlagOptions: [], //主机类型
      hostOlineOptions: [], //在线状态
      hostStateOptions: [], //主机状态
      hostSecOptions: [], //主机密级
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询【主机】列表 */
    getList() {
      this.loading = true;
      listHost(this.queryParams).then(response => {
        this.hostList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hostId: null,
        hostName: null,
        hostFlag: null,
        hostOline: null,
        hostState: null,
        version: null,
        hostIp: null,
        hostMac: null,
        hostSn: null,
        hostSec: null,
        hostRegDate: null,
        hostActiveDate: null,
        hostUnregDate: null,
        lastModifyDate: null,
        userId: null,
        hostCode: null,
        remark: null,
        feature: null,
        datasource: null,
        datasourceId: null,
        isDelete: null,
        hostSys: null,
        lastOnlineDate: null,
        osversioninfo: null,
        createDate: null,
        hostIpval: null,
        osInstalltime: null,
        hostAssetsNo: null,
        hostRoomNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hostId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【主机】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hostId = row.hostId || this.ids
      getHost(hostId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【主机】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hostId != null) {
            updateHost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hostIds = row.hostId || this.ids;
      const hostName = row.hostName || this.hostName;
      this.$modal.confirm('是否确认删除【主机】名称为"' + hostName + '"的数据项？').then(function() {
        return delHost(hostIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/host/export', {
        ...this.queryParams
      }, `host_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
